package com.wt.admin.service.ai.impl.agents;

import com.wt.admin.domain.dto.ai.AgentsInfoDTO;
import com.wt.admin.domain.entity.ai.ModelConfigEntity;
import com.wt.admin.service.ai.impl.tools.DateTimeTools;
import com.wt.admin.service.ai.impl.tools.WeatherTools;
import io.micrometer.observation.Observations;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.ollama.management.ModelManagementOptions;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbacks;
import org.springframework.stereotype.Component;

@Component("Ollama")
public class OllamaBuilder implements AgentsBuilderService{

    @Resource
    ToolCallingManager toolCallingManager;

    @Override
    public ChatClient builder(AgentsInfoDTO agents, ModelConfigEntity model) {
        // 查询模型信息
        var ollamaApi = new OllamaApi(model.getBaseUrl());
        ChatModel ollama = new OllamaChatModel(ollamaApi , OllamaOptions.builder()
                .model(model.getModel())
                .temperature(0.7)
                .build(),toolCallingManager, Observations.getGlobalRegistry(), ModelManagementOptions.defaults());
        // 不建议使用工具，1. 很多大模型不支持 2. 不方便与外部建立连接
        ToolCallback[] dateTimeTools = ToolCallbacks.from(new DateTimeTools(),new WeatherTools());
        return ChatClient.builder(ollama)
//                .defaultTools(dateTimeTools)
                .defaultSystem("""
                        你是%s,%s
                        """.formatted(agents.getName(),agents.getBackground())).build()
                .mutate()
                .build();
    }


}
